<template>
	<div class="system-dispatch-container standard-container layout-padding layout-pd">
		<div class="review-list-title">
			<div class="title-actions">
				<el-button size="default" type="primary" class="ml10" @click="getTableData">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="primary" class="ml10" @click="onOpen('add', '')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增模版
				</el-button>

				<!-- <el-row :gutter="10">
					<el-col :xs="24" :sm="6" :md="6" :lg="6" :xl="6">
						<el-form-item label="模版名称:">
							<el-input v-model="state.search.name" size="default" clearable placeholder="请输入活动名称" style="width: 100%"> </el-input>
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="8" :md="8" :lg="8" :xl="8" v-show="state.isToggle">
						<el-form-item label="创建时间:">
							<el-date-picker
								type="datetimerange"
								v-model="state.search.searchDateTime"
								start-placeholder="创建开始时间"
								end-placeholder="创建时间"
								style="width: 100%"
							/>
						</el-form-item>
					</el-col>

					<el-col :xs="24" :sm="12" :md="8" :lg="8" :xl="8">
						<el-form-item class="table-form-btn" label-width="10px">
							<div class="table-form-btn-toggle ml10 cursor-pointer" @click="state.isToggle = !state.isToggle">
								<span>{{ state.isToggle ? '收筛选' : '展筛选' }}</span>
								<SvgIcon :name="state.isToggle ? 'ele-ArrowUp' : 'ele-ArrowDown'" />
							</div>
							<el-button size="default" type="primary" class="ml10" @click="getTableData">
								<el-icon>
									<ele-Search />
								</el-icon>
								查询
							</el-button>
							<el-button size="default" type="primary" class="ml10" @click="onOpen('add', '')">
								<el-icon>
									<ele-FolderAdd />
								</el-icon>
								新增模版
							</el-button>
						</el-form-item>
					</el-col>
				</el-row> -->
			</div>
		</div>
		<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%" @expand-change="dataChange" row-key="id">
			<el-table-column type="expand">
				<template #default="props">
					<div class="rate-area-title"><el-text tag="i" size="lager">配送区域</el-text></div>
					<el-table :data="props.row.areaRates">
						<el-table-column label="可配送区域" prop="name">
							<template #default="scope">
								<span v-for="(item, index) in scope.row.areas" :key="index"> {{ item.name }},</span>
							</template>
						</el-table-column>
						<el-table-column :label="props.row.calType == 0 ? '首件' : '首重(kg)'" prop="firstCount"> </el-table-column>
						<el-table-column label="运费(元)" prop="firstRate" />
						<el-table-column label="续件" prop="secondCount" />
						<el-table-column label="续费(元)" prop="secondRate" />
					</el-table>
				</template>
			</el-table-column>
			<el-table-column prop="dispatchName" label="模版名称" show-overflow-tooltip></el-table-column>
			<el-table-column prop="calType" label="计费类型" show-overflow-tooltip>
				<template #default="scope">
					<el-tag v-if="scope.row.calType == 0">按件数</el-tag>
					<el-tag v-else-if="scope.row.calType == 1">按重量</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="createTime" label="创建时间" show-overflow-tooltip></el-table-column>
			<el-table-column prop="updateTime" label="修改时间" show-overflow-tooltip></el-table-column>
			<el-table-column prop="updateUsername" label="操作人" show-overflow-tooltip></el-table-column>
			<el-table-column label="操作" width="200">
				<template #default="scope">
					<el-button :disabled="scope.row.roleName === '超级管理员'" size="small" text type="primary" @click="onOpen('edit', scope.row.id)"
						>修改</el-button
					>
					<el-button :disabled="scope.row.roleName === '超级管理员'" size="small" text type="primary" @click="onCopy(scope.row)">复制</el-button>
					<el-button :disabled="scope.row.roleName === '超级管理员'" size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div class="pagination-container">
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				:page-sizes="[10, 15, 20, 30, 50, 100]"
				v-model:current-page="state.tableData.paging.currentPage"
				background
				v-model:page-size="state.tableData.paging.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.paging.totalCount"
			>
			</el-pagination>
		</div>

		<DispatchDialog ref="DispatchDialogRef" @refresh="onRefresh"></DispatchDialog>
	</div>
</template>

<script setup lang="ts">
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { ElMessageBox } from 'element-plus';
import { dispatchApi } from '/@/api/shop/dispatch/index';
import messageUtils from '/@/utils/messageUtils';
import { resolveEndTime, resolveStartTime } from '/@/utils/time/utils';
const DispatchDialog = defineAsyncComponent(() => import('./dialog.vue'));
const DispatchDialogRef = ref();
const state = reactive({
	isToggle: false,
	search: {
		name: '',
		searchDateTime: '',
	},
	tableData: {
		data: [],
		total: 0,
		loading: false,
		paging: {
			currentPage: 1,
			pageSize: 15,
			totalCount: 0,
			totalPages: 0,
		},
	},
});

const onCopy = (row: any) => {
	messageUtils().warn('复制功能还在进行中,敬请期待!!!!!');
	console.log('row: ', row);
};

const onOpen = (type: string, id: string) => {
	DispatchDialogRef.value.openDialog(type, id);
};

const dataChange = async (row: any, expandRows: any[]) => {
	console.log('row:', row, ' ,expandRows: ', expandRows);
	const { data } = await dispatchApi().rateAares(row.id);
	console.log('data: ', data);
	row.areaRates = data;
};

const onRowDel = (row: any) => {
	ElMessageBox.confirm(`此操作将永久删除发货模版：“${row.dispatchName}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			try {
				await dispatchApi().delete(row.id);
				messageUtils().success('删除成功');
				onRefresh();
			} catch (error: any) {
				messageUtils().error('删除失败，请重试');
			}
		})
		.catch(() => {});
};

// 分页改变
const onHandleSizeChange = (pageSize: number) => {
	state.tableData.paging.pageSize = pageSize;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (currentPage: number) => {
	state.tableData.paging.currentPage = currentPage;
	getTableData();
};

const getTableData = async () => {
	try {
		state.tableData.loading = true;
		const { data } = await dispatchApi().search({
			dispatchName: state.search.name,
			startTime: resolveStartTime(state.search.searchDateTime),
			endTime: resolveEndTime(state.search.searchDateTime),
			offset: (state.tableData.paging.currentPage - 1) * state.tableData.paging.pageSize,
			limit: state.tableData.paging.pageSize,
		});
		state.tableData.data = data.datas;
		state.tableData.paging.currentPage = data.paging.currentPage;
		state.tableData.paging.totalCount = data.paging.totalCount;
		console.log('data: ', data);
	} catch (ex) {
		messageUtils().error('加载失败请重试');
	} finally {
		state.tableData.loading = false;
	}
};

const onRefresh = () => {
	onHandleCurrentChange(1);
};

onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
.rate-area-title {
	// margin-left: 16px;
	padding-left: 16px;
	width: 100%;
	background-color: rgba(236, 240, 241, 1);
	height: 30px;
	line-height: 30px;
}

.system-dispatch-container {
	.el-table {
		flex: 1;
	}
}
</style>
